/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.pc.domain.domains.security;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.opu.pc.domain.domains.AbstractDomain;

/**
 * 
 * @author Yuriy Dunko
 */
@Entity
@Table(name = "users")
public class User implements AbstractDomain<Integer> {

	private static final long serialVersionUID = 2039369207437117676L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;

	@Column(name = "name", nullable = false, unique = true)
	private String name;

	@Column(name = "password", nullable = false)
	private String password;

	@Column(name = "email")
	private String email;
	
	@ManyToMany(fetch=FetchType.EAGER)
    @JoinTable(name = "user_security",
        joinColumns=
            @JoinColumn(name = "user_id", referencedColumnName = "id"),
        inverseJoinColumns=
            @JoinColumn(name = "security_groups_id", referencedColumnName = "id"))
    private Set<SecurityGroupHolder> securityGroups = new HashSet<SecurityGroupHolder>();

	
	public User(Integer id, String name, String password, String email) {
		this.id = id;
		this.name = name;
		this.password = password;
		this.email = email;
	}

	public User(String name, String password, String email) {
		this(null, name, password, email);
	}

	public User(String name, String password) {
		this(null, name, password, "");
	}

	public User() {
	}

	public User(Integer id) {
        this.id = id;
	}

	@Override
	public Integer getId() {
		return id;
	}

	@Override
	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Set<SecurityGroupHolder> getSecurityGroups() {
		return securityGroups;
	}

	public void setSecurityGroups(Set<SecurityGroupHolder> securityGroups) {
		this.securityGroups = securityGroups;
	}
	
	public boolean availableFor(SecurityGroup securityGroup){
		return securityGroups.contains(new SecurityGroupHolder(securityGroup));
	}
	
	public boolean availableAtLeastForOne(Collection<SecurityGroup> securityGroups){
		for (SecurityGroup securityGroup : securityGroups) {
			if (availableFor(securityGroup)){
				return true;
			}
		}
		return false;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

    @Override
    public String toString() {
        return name + " " + securityGroups;
    }
}
